﻿
using System;
using System.Collections.Generic;
using System.Web;
using WeiTap.Restful;
using BrnMall.Manager.Domain.User;
using BrnMall.Manager.Manager.User;
using QIDong.WeApp.Models;

namespace QIDong.WeApp.Controllers
{
    public class UserCashController : WebApi
    {
        #region 余额

        /// <summary>
        /// 获取用户余额
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <returns></returns>
        public UserCash GetUserCash(int uid = 0)
        {
            UserCash model = null;
            try
            {
                model = UserCashManager.GetInfo(uid);
                if (model == null)
                    model = BrnMall.Services.CashOrders.GetDefaultUserCash(uid);

                model.Cash = BrnMall.Services.CashOrders.DecodeCash(model.Cash).ToString();
            }
            catch (Exception e)
            {
                throw new ApiException(ExceptionCode.DataError, e.Message);
            }
            return model;
        }

        /// <summary>
        /// 测试充值回调
        /// </summary>
        /// <param name="oid"></param>
        /// <param name="paySN"></param>
        /// <returns></returns>
        public string TestPayOrder(int oid, string paySN)
        {
            BrnMall.Services.CashOrders.PayOrder(oid, BrnMall.Core.OrderState.Confirmed, paySN, DateTime.Now);
            return "";
        }
        #endregion

        #region 充值操作

        /// <summary>
        /// 添加充值订单
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="storeId"></param>
        /// <param name="amount"></param>
        /// <param name="cashItemId"></param>
        /// <returns></returns>
        public string SubmitOrder(int uid, int storeId, decimal amount)
        {
            CashOrder model = GetCashOrder(uid, storeId, amount);
            if (amount < 0)
                throw new Exception("金额不能小于0");

            CashItem item = null;
            //获取赠送活动，并且按充值金额倒序
            List<CashItem> list = CashItemManager.GetListByAmount(storeId, 50);
            foreach (var info in list)
            {
                if (amount >= info.Amount)
                {
                    item = info;    //根据金额获取赠送选项
                    break;
                }
            }

            if (item != null)
            {
                if (item.StoreId != storeId)
                    throw new Exception("不允许使用其他商户的赠送活动");

                //设置赠送金额
                if (amount >= item.Amount)
                    model.GiveAmount = item.GiveAmount;
            }

            CashOrderManager.Add(model);
            return model.id.ToString();
        }

        CashOrder GetCashOrder(int uid, int storeId, decimal amount)
        {
            CashOrder model = new CashOrder()
            {
                Uid = uid,
                StoreId = storeId,
                OrderAmount = amount,
                GiveAmount = 0,
                CashItemId = 0,
                CreateTime = DateTime.Now
            };
            model.OSN = BrnMall.Services.Orders.GenerateOSN(storeId, uid, 0, DateTime.Now);
            model.OrderState = (int)BrnMall.Core.OrderState.WaitPaying; //等待付款状态
            model.IP = BrnMall.Core.WebHelper.GetIP();
            return model;
        }

        #endregion

        #region 赠送选项

        /// <summary>
        /// 获取现金充值的赠送选项
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="pageSize">返回最多条数</param>
        /// <returns></returns>
        public CashItemListModel CashItemList(int storeId, int pageSize = 10)
        {
            List<CashItem> list = CashItemManager.GetList(storeId, 1, pageSize);

            CashItemListModel model = new CashItemListModel();
            model.List = new List<CashItemTitle>();
            foreach (var item in list)
            {
                CashItemTitle titleModel = new CashItemTitle() { id = item.id, StoreId = item.StoreId, Amount = item.Amount, GiveAmount = item.GiveAmount };
                if (item.GiveAmount > 0)
                    titleModel.title = string.Format("充{0}，送{1}", item.Amount, item.GiveAmount);
                else
                    titleModel.title = string.Format("充{0}", item.Amount);
                model.List.Add(titleModel);
            }
            model.PageModel = new PageModel(10, 1, list.Count);

            return model;
        }

        #endregion

        #region 充值记录

        /// <summary>
        /// 获取充值记录
        /// </summary>
        /// <param name="uid">会员ID</param>
        /// <param name="direction"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public CashRecordListModel CashRecordList(int uid, int direction = 0, int page = 1, int pageSize = 15)
        {
            var model = new CashRecordListModel()
            {
                List = CashRecordManager.GetList(uid, direction, page, pageSize)
            };
            model.PageModel = new PageModel(pageSize, page, (int)model.List.TotalItems);
            return model;
        }
        #endregion
    }
}